package com.myapplication.zhou.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.myapplication.adapter.SortPageRecycleViewAdapter;
import com.myapplication.adapter.SortPageRightRecycleViewAdapter;
import com.myapplication.callback.SortPageCallback;
import com.myapplication.interfaces.SortPageConnect;
import com.myapplication.interfaces.SortPageInter;
import com.myapplication.model.SortGoods;
import com.myapplication.zhou.alldemo.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SortTopViewFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SortTopViewFragment extends Fragment implements SortPageCallback, SortPageRecycleViewAdapter.MyViewHolderOnClickListener {


    @ViewInject(R.id.textView_title)
    private TextView textView_title;
    private SortPageInter sortPageInter;
    private SortPageRecycleViewAdapter sortPageRecycleViewAdapter;
    private SortPageRightRecycleViewAdapter sortPageRightRecycleViewAdapter;
    private TextView textViewtitle;
    @ViewInject(R.id.recycleView1)
    private RecyclerView recycleView1;
    @ViewInject(R.id.recycleView2)
    private RecyclerView recycleView2;
    private List<SortGoods> sortGoodsList = new ArrayList<>();
    private String[] text_array = null;
    private String[] gcc_id_array = null;

    public SortTopViewFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment SortTopViewFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SortTopViewFragment newInstance() {
        SortTopViewFragment fragment = new SortTopViewFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sort_top_view, container, false);
        x.view().inject(this, view);
        textView_title.setText("商品分类");
        sortPageInter = new SortPageConnect();
        sortPageInter.getSortPageInfo(this);
        sortPageRecycleViewAdapter = new SortPageRecycleViewAdapter(getContext(), sortGoodsList);
        sortPageRightRecycleViewAdapter = new SortPageRightRecycleViewAdapter(getContext(), text_array, gcc_id_array);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, LinearLayoutManager.VERTICAL, false);
        recycleView1.setLayoutManager(layoutManager);
        recycleView1.setNestedScrollingEnabled(false);
        recycleView1.setAdapter(sortPageRecycleViewAdapter);
        recycleView2.setLayoutManager(gridLayoutManager);
        recycleView2.setAdapter(sortPageRightRecycleViewAdapter);
        sortPageRecycleViewAdapter.setOnItemClickListener(this);
        return view;

    }

    @Override
    public void onSortPageCallbackSuccess(String sortPageSuccess) {
        Log.i("JSON", sortPageSuccess);
        paraseJsonOnSort(sortPageSuccess);

    }

    private void paraseJsonOnSort(String sortPageSuccess) {
        try {
            JSONObject jsonObject = new JSONObject(sortPageSuccess);
            JSONObject jsonObject1 = jsonObject.optJSONObject("datas");
            JSONArray jsonArray = jsonObject1.optJSONArray("class_list");
            Gson gson = new Gson();
            Type type = new TypeToken<List<SortGoods>>() {
            }.getType();
            String json = jsonArray.toString();
            sortGoodsList = gson.fromJson(json, type);
            Log.i("jsjsjk", sortGoodsList.get(1).getGc_name());
            sortPageRecycleViewAdapter.update(sortGoodsList);
            text_array = sortGoodsList.get(0).getText().split("!");
            gcc_id_array = sortGoodsList.get(0).getGcc_id().split("/");
            String b = "";
            String c = "";
            for(int i=0;text_array.length>i;i++){
                b = b+text_array[i];
                c = c+gcc_id_array[i];
            }
            Log.i("text_array",b);
            Log.i("gcc_id_array",c);
            sortPageRightRecycleViewAdapter.update(text_array, gcc_id_array);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSortPageCallbackFail(String sortPageFail) {

    }

    @Override
    public void onClick(View view, SortGoods sortGoods, int position) {
        sortPageRecycleViewAdapter.changeData(position);
        Log.i("position-----",position+"------");
        text_array = sortGoods.getText().split("!");
        gcc_id_array = sortGoods.getGcc_id().split("/");
        sortPageRightRecycleViewAdapter.update(text_array, gcc_id_array);
    }
}
